package com.ssd.service.impl;

import com.ssd.api.user.dto.AddressBook;
import com.ssd.mapper.AddressBookMapper;
import com.ssd.service.IAddressBookService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ssd.utils.UserContext;
import com.ssd.view.BaseResponse;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 地址薄 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-11-14
 */
@Service
public class AddressBookServiceImpl extends ServiceImpl<AddressBookMapper, AddressBook> implements IAddressBookService {

    /**
     * 设置默认地址
     * @param addressBook 里面有地址id
     * @return
     */
    @Override
    @Transactional
    public BaseResponse putDefault(AddressBook addressBook) {
        //1.获取地址id
        Long addressBookId = addressBook.getId();
        //2.获取当前用户id
        Long userId = UserContext.currentUserId();
        //3.查询当前用户是否有默认地址
        AddressBook addressBook1 = lambdaQuery()
                .eq(AddressBook::getUserId, userId)
                .eq(AddressBook::getIsDefault, 1)
                .one();
        if (null != addressBook1){
            //判断是否是同一个地址
            if (addressBook1.getId().equals(addressBookId)){
                return new BaseResponse(true);
            }
            //修改该地址为非默认地址
            boolean update = lambdaUpdate().eq(AddressBook::getId, addressBook1.getId())
                    .set(AddressBook::getIsDefault, 0)
                    .update();
            if (!update){
                return new BaseResponse(false,"修改默认地址失败！");
            }
        }
        //设置当前用户的地址为默认地址
        boolean flag = lambdaUpdate()
                .eq(AddressBook::getUserId, userId)
                .eq(AddressBook::getId,addressBookId)
                .set(AddressBook::getIsDefault, 1)
                .update();
        return new BaseResponse(flag);
    }

    /**
     * 获取用户默认地址
     *
     * @return
     */
    @Override
    public AddressBook getDefault() {
        //根据用户id和1查询用户默认地址
        AddressBook addressBook = lambdaQuery().eq(AddressBook::getUserId, UserContext.currentUserId())
                .eq(AddressBook::getIsDefault, 1).one();
        return addressBook;
    }

    /**
     * 新增用户地址
     * @param addressBook 地址数据
     * @return
     */
    @Override
    public BaseResponse addAddressBook(AddressBook addressBook) {
        //设置用户id和默认不启用
        addressBook.setUserId(UserContext.currentUserId());
        addressBook.setIsDefault(0);
        return new BaseResponse(save(addressBook));
    }

    /**
     * 根据id修改地址
     * @param addressBook 地址数据
     * @return
     */
    @Override
    @Transactional
    public BaseResponse putAddressBook(AddressBook addressBook) {
        //1.判断是否要改成默认地址
        if (addressBook.getIsDefault().equals(1)){
           //查出默认地址改为非默认
            AddressBook addressBook1 = lambdaQuery()
                    .eq(AddressBook::getUserId, UserContext.currentUserId())
                    .eq(AddressBook::getIsDefault, 1)
                    .one();
            if (null != addressBook1){
                boolean update = lambdaUpdate().eq(AddressBook::getId, addressBook1.getId())
                        .set(AddressBook::getIsDefault, 0)
                        .update();
                if (!update){}
            }
        }
        //2.修改地址
        return new BaseResponse(updateById(addressBook));
    }
}
